<?php
    require_once('config.php');
    $url = $_POST['url'];
    unset($_POST['url']);
    if(count($_POST)){
        try{
            $pdo = new PDO("mysql:host=".mysqlAddress.";dbname=".mysqlDB,mysqlUser,mysqlPassword);
            $pdo->query("set names utf8");
            $data = array();
            $sql = "select `".columnName."`,`".columnType."`,`".columnSaveUrl."` from ".mysqlTable." where `".columnName."` in (";
            foreach($_POST as $v){
                $sql .= "'".$v."',";
            }
            $sql = rtrim($sql,',');
            $sql .= ") and ".columnSaveUrl."='".$url."'";
            $pdo->beginTransaction();
            $re = $pdo->query($sql);
            $arr = array();
            $sql = "delete from ".mysqlTable." where";
            $sql1 = " `".columnSaveUrl."` in (";
            $sql2 = " (`".columnName."` in (";
            while($row = $re->fetch()){
                if($row['type']){
                    $sql .= " `".columnSaveUrl."` like '".$row['url']."/".$row['name']."/%' or";
                    $sql1 .= "'".$row['url']."/".$row['name']."',";
                }
                $sql2 .= "'".$row['name']."',";
            }
            $sql1 = rtrim($sql1,',');
            $sql2 = rtrim($sql2,',');
            $sql2 .= ") and `".columnSaveUrl."` = '".$url."')";
            $sql1 .= ") or";
            $sql .= $sql1;
            $sql .= $sql2;
            $pdo->query($sql);
            $pdo->commit();
            foreach($_POST as $v){
                if(is_dir($url.'/'.$v)){
                    $dh=opendir($url.'/'.$v);
                    while ($file=readdir($dh)) {
                        if($file!="." && $file!="..") {
                            $fullpath=$url.'/'.$v.'/'.$file;
                            if(!is_dir($fullpath)) {
                                unlink($fullpath);
                            } else {
                                rmdir($fullpath);
                            }
                        }
                    }

                    closedir($dh);
                    //删除当前文件夹：
                    rmdir($url.'/'.$v);
                }else{
                    unlink($url.'/'.$v);
                }
            }
            $arr = array(
                'success'=>"成功",
            );
            echo json_encode($arr);
        }catch(PDOException $ex){
            $arr = array(
                'error'=>"数据库未知错误请打印sql语句排查",
            );
            $pdo->rollBack();
            echo json_encode($arr);

        }
    }else{
        $arr = array(
            'error'=>"请选择删除的文件",
        );
        echo json_encode($arr);
    }
